<template>
  <BasicTable @register="registerTable">
    <template #toolbar>
      <a-button
        type="primary"
        v-if="hasPermission(['sysDictData:add'])"
        @click="handleCreate"
      >
        {{ t('新增字典类型') }}
      </a-button>
    </template>
    <template #action="{ record }">
      <TableAction
        :actions="[
          {
            icon: 'clarity:note-edit-line',
            auth: 'sysDictData:edit',
            tooltip: t('编辑字典类型'),
            onClick: handleEdit.bind(null, record),
          },
          {
            icon: 'ant-design:delete-outlined',
            auth: 'sysDictData:delete',
            color: 'error',
            tooltip: t('删除字典类型'),
            popConfirm: {
              title: t('是否确认删除'),
              confirm: handleDelete.bind(null, record),
            },
          },
        ]"
      />
    </template>
  </BasicTable>
  <DictDataModal
    :typeId="typeId"
    @register="registerDataModal"
    @success="handleSuccess"
  />
</template>
<script lang="ts">
import { defineComponent, defineExpose, ref } from 'vue';
import { usePermission } from '@/hooks/web/usePermission';
import { useModal, useModalInner } from '@/components/Modal';
import { useMessage } from '@/hooks/web/useMessage';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import {
  getSysDictDataList,
  deleteSysDictData,
} from '@/api/sys/sysDictDataManage';
import { dataColumns, searchDataFormSchema } from './dict.data';
import DictDataModal from './DictDataModal.vue';
import { useI18n } from '@/hooks/web/useI18n';
export default defineComponent({
  name: 'DeptModal',
  components: { BasicTable, TableAction, DictDataModal },
  emits: ['success', 'register'],
  setup(props, {}) {
    const { t } = useI18n();
    const { hasPermission } = usePermission();
    const { createMessage } = useMessage();
    const [registerTable, { getForm, reload }] = useTable({
      title: t('字典明细列表'),
      api: getSysDictDataList,
      columns: dataColumns,
      immediate: false,
      formConfig: {
        labelWidth: 120,
        baseColProps: { span: 24 },
        schemas: searchDataFormSchema,
      },
      pagination: false,
      useSearchForm: true,
      bordered: true,
      striped: true,
      showIndexColumn: false,
      actionColumn: {
        ifShow: hasPermission(['sysDictData:edit', 'sysDictData:delete']),
        width: 120,
        title: t('操作'),
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
      beforeFetch: (params) => {
        params.typeId = typeId.value;
        return params;
      },
    });
    const [registerDataModal, { openModal }] = useModal();
    const [registerModal, { setModalProps, closeModal }] = useModalInner(
      (data) => {
        setModalProps({ confirmLoading: false });
        reload({
          searchInfo: {
            typeId: data.record.id,
          },
        });
      }
    );

    function handleCreate() {
      openModal(true, {
        isUpdate: false,
      });
    }

    function handleEdit(record: Recordable) {
      openModal(true, {
        record,
        isUpdate: true,
      });
    }

    function handleDelete(record: Recordable) {
      deleteSysDictData([record.id]).then(() => {
        createMessage.success(t('删除成功'));
        reload();
      });
    }

    function handleSuccess() {
      reload();
    }
    const typeId = ref('');
    function loadDictData(pid) {
      typeId.value = pid;
      reload({
        searchInfo: {
          typeId: pid,
        },
      });
    }
    defineExpose({ loadDictData });
    return {
      t,
      registerTable,
      registerModal,
      registerDataModal,
      handleCreate,
      handleEdit,
      handleDelete,
      handleSuccess,
      closeModal,
      typeId,
      loadDictData,
      hasPermission,
    };
  },
});
</script>
